<template>
  <div class="container">
    <div class="header">
      <div class="header-left">
        <div class="hotel-title">CWHOTEL</div>
        <div class="hotel-phone">电话: 188-8230-6002</div>
      </div>
      <div class="header-middle">
        <div class="category-links">
          <span
              v-for="(type, index) in roomTypes"
              :key="type"
              class="category-link"
              :class="{ 'active': type === category }"
              @click="selectCategory(type)"
          >
            <span v-if="type === category" style="font-weight: bold;">{{ type }}</span>
            <span v-else>{{ type }}</span>
            <span v-if="index < roomTypes.length - 1" style="margin-right: 10px;"></span>
          </span>
        </div>
        <div class="price-range">
          <label>价格范围:</label>
          <input type="number" v-model.number="minPrice" placeholder="最低价" class="price-input">
          <input type="number" v-model.number="maxPrice" placeholder="最高价" class="price-input">
        </div>
      </div>
      <div class="header-right">
        <a href="#" @click.prevent="goToLogin">登录</a>
        <a href="#" @click.prevent="goToRegister">注册</a>
      </div>
    </div>
    <div class="card-container">
      <el-card
          v-for="room in filteredTableData"
          :key="room.id"
          :style="{ width: getCardWidth() + 'px' }"
          class="room-card"
          @click="goToDetails(room.id)"
      >
        <img v-lazy="getRoomImageUrl(room)" alt="Room Image" class="room-image"/>
        <div class="room-details">
          <div class="room-name">{{ room.roomName }}</div>
          <div class="room-category">{{ room.category }}</div>
          <div class="room-price">{{ room.price }}元</div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import router from "@/router";
import VueLazyload from 'vue-lazyload';

export default {
  name: "CheckIn",
  components: {},
  data() {
    return {
      search: '',
      currentPage: 1,
      total: 10,
      pageSize: 20,
      tableData: [],
      category: '',
      minPrice: null,
      maxPrice: null,
      roomTypes: [
        '大床房',
        '海景房',
        '商务房',
        '豪华型',
        '标准型',
        '家庭型',
        '特色型',
        '商务型',
        '经济型'
      ],
      showCategorySelect: false
    }
  },
  computed: {
    filteredTableData() {
      let rooms = this.tableData;
      if (this.category) {
        rooms = rooms.filter(room => room.category === this.category);
      }
      if (this.minPrice !== null || this.maxPrice !== null) {
        rooms = rooms.filter(room =>
            (!this.minPrice || room.price >= this.minPrice) &&
            (!this.maxPrice || room.price <= this.maxPrice)
        );
      }
      return rooms;
    }
  },
  methods: {
    handleSizeChange() {
      this.load();
    },
    handleCurrentChange() {
      this.load();
    },
    load() {
      request.get("/user/getHomeCardPage", {
        params: {
          pageNumber: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    goToDetails(id) {
      this.$router.push({path: '/HotelRoomInfo', query: {id: id}});
    },
    getCardWidth() {
      const windowWidth = window.innerWidth;
      if (windowWidth >= 1200) {
        return 250;
      } else if (windowWidth >= 992) {
        return 300;
      } else if (windowWidth >= 768) {
        return 400;
      } else {
        return windowWidth * 0.9;
      }
    },
    searchHomeCategory() {
      request.get("/user/selectCategoryPage", {
        params: {
          category: this.category
        }
      }).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    getRoomImageUrl(room) {
      return room.coverImage.startsWith('http') ? room.coverImage : require(`@/${room.coverImage}`);
    },
    selectCategory(type) {
      this.category = type;
    },
    goToLogin() {
      this.$router.push('/UserLogin');
    },
    goToRegister() {
      this.$router.push('/UserRegister');
    }
  },
  created() {
    this.load();
    window.addEventListener('resize', this.getCardWidth);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.getCardWidth);
  }
};
</script>

<style scoped>
.container {
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f5f5, #e0e0e0); /* 渐变背景色 */
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  box-sizing: border-box;
}

.header {
  width: 100%;
  max-width: 1200px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background-color: #f0f4f8; /* 柔和的浅蓝色背景 */
  border-bottom: 1px solid #e0e0e0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: box-shadow 0.3s ease;
}

.header:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.header-left {
  display: flex;
  align-items: center;
}

.hotel-title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  background: linear-gradient(90deg, #ff5722, #ff8a65);
  -webkit-background-clip: text;
  color: transparent;
  animation: gradient 3s infinite;
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.hotel-phone {
  margin-left: 20px;
  color: #777;
  font-size: 14px;
}

.header-middle {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-grow: 1;
}

.category-links {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.category-link {
  transition: all 0.3s ease;
  cursor: pointer;
  padding: 10px 20px;
  border-radius: 4px;
  background-color: #f0f0f0;
  flex-basis: 100px;
  text-align: center;
  color: #333;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.category-link:hover,
.category-link.active {
  color: #fff;
  background-color: #ff5722;
  transform: scale(1.05);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.price-range {
  display: flex;
  align-items: center;
}

.price-range label {
  margin-right: 10px;
  font-weight: bold;
  color: #333;
}

.price-range .price-input {
  margin-right: 10px;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  width: 100px;
  text-align: center;
  background-color: #f9f9f9;
  color: #333;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.price-range .price-input:focus {
  border-color: #ff5722;
  box-shadow: 0 0 5px rgba(255, 87, 34, 0.5);
}

.header-right {
  display: flex;
  align-items: center;
}

.header-right a {
  margin-left: 20px;
  color: #ff5722;
  font-size: 16px;
  text-decoration: none;
  transition: color 0.3s ease;
}

.header-right a:hover {
  color: #ff8a65;
}

.card-container {
  width: 100%;
  max-width: 1200px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
}

.room-card {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s ease-in-out, box-shadow 0.3s ease-in-out, border 0.3s ease-in-out;
  cursor: pointer;
  background-color: #fff;
  position: relative;
  overflow: hidden;
}

.room-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0));
  opacity: 0;
  transition: opacity 0.3s ease;
}

.room-card:hover::before {
  opacity: 1;
}

.room-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  border: 2px solid #ff5722; /* 添加橙色边框 */
}

.room-image {
  width: 100%;
  height: 150px;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.room-card:hover .room-image {
  transform: scale(1.1);
}

.room-details {
  padding: 10px;
  background-color: white;
  position: relative;
  transition: all 0.3s ease;
}

.room-name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 5px;
  color: #333;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1); /* 文字阴影 */
  transition: color 0.3s ease;
}

.room-card:hover .room-name {
  color: #ff5722; /* 悬停时文字颜色变化 */
}

.room-category {
  color: #777;
  margin-bottom: 5px;
  font-size: 14px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1); /* 文字阴影 */
  transition: color 0.3s ease;
}

.room-card:hover .room-category {
  color: #ff5722; /* 悬停时文字颜色变化 */
}

.room-price {
  color: #ff5722;
  font-weight: bold;
  font-size: 16px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1); /* 文字阴影 */
  transition: color 0.3s ease;
}

.room-card:hover .room-price {
  color: #ff8a65; /* 悬停时文字颜色变化 */
}

.pagination {
  width: 100%;
  max-width: 1200px;
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 767px) {
  .room-card {
    width: 100% !important;
  }

  .room-image {
    height: 200px;
  }
}
</style>